package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.query.SearchQuery;
import cn.wolfcode.wolf2w.business.service.ISearchService;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import org.apache.poi.sl.usermodel.Notes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class SearchController {

    @Autowired
    private ISearchService searchService;

    @GetMapping("/search")
    public R<?> search(SearchQuery qo) throws UnsupportedEncodingException, InvocationTargetException, IllegalAccessException {
        //转码,将浏览器中传递的转换成指定格式
        String keyword = URLDecoder.decode(qo.getKeyword(), "utf-8");
        //转换后重新进行转换到原始数据
        qo.setKeyword(keyword);
        switch (qo.getType()){
            case 1:
                return R.ok(searchStrategy(qo));
            case 2:
                return R.ok(searchNote(qo));
            case 3:
                return R.ok(searchUser(qo));
            case 4:
                return R.ok(searchAnswer(qo));
            default:
                return R.ok(searchAll(qo));
        }
    }

    private Object searchAll(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        Map<String,Object> map = new HashMap<>();
        Page<UserInfo> userPage = searchService.searchHighLight(UserInfoEs.class, UserInfo.class, qo, "info", "nickname", "city");
        Page<Strategy> strategyPage = searchService.searchHighLight(StrategyEs.class, Strategy.class, qo, "title", "subTitle", "summary");
        Page<Note> notesPage = searchService.searchHighLight(NoteEs.class, Note.class, qo, "title", "summary");
        Page<Destination> destinationPage = searchService.searchHighLight(DestinationEs.class, Destination.class, qo, "name", "info");
        Long total = userPage.getTotalElements() + strategyPage.getTotalElements()
                + notesPage.getTotalElements() + destinationPage.getTotalElements();
        map.put("total",total);
        map.put("users",userPage.getContent());
        map.put("notes",notesPage.getContent());
        map.put("strategies",strategyPage.getContent());
        map.put("dests",destinationPage.getContent());
        return map;
    }

    private Object searchAnswer(SearchQuery qo) {
        return null;
    }

    private Object searchUser(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {

        return searchService.searchHighLight(UserInfoEs.class,UserInfo.class,qo,"info","nickname","city");
    }

    private Object searchNote(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        return  searchService.searchHighLight(NoteEs.class, Note.class, qo, "title", "summary");
    }
    private Object searchStrategy(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {

        return searchService.searchHighLight(StrategyEs.class, Strategy.class,qo,"title","subTitle","summary");
    }
}